package com.cpit.qualitycontrol.schedule;

import com.cpit.platform.common.utils.FileUtil;
import com.cpit.platform.config.UploadPathConfig;
import com.cpit.platform.log.LogOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Configuration
@EnableScheduling // 启用定时任务
public class QcSchedulingConfig {

    private static final String loggerName = QcSchedulingConfig.class.getName();

    @Autowired
    private UploadPathConfig uploadPath;

    @Scheduled(cron = "0 0 1 * * ?") //每天1时删除临时目录
    public void scheduler() {

        // delete ./upload/temp/*.*
        try {
            FileUtil.forceDeleteDirectories(uploadPath.getTemp());
        } catch (IOException e) {
            LogOperator.err(loggerName, "delete ./upload/temp/*.* Directories error!", e);
        }

        // delete ./upload/unqualified
        /*Long currentTimeMillis = System.currentTimeMillis();
        File files = new File(uploadPath.getUnqualified());
        for (File fileTemp : files.listFiles()) {
            if (fileTemp.isFile()) {
                Long updateFileTime = fileTemp.lastModified();
                if (((currentTimeMillis - updateFileTime) > 1000 * 60 * 60 * 24 * 365)) {
                    fileTemp.delete();
                }
            }
        }*/
    }
}
